<?php
namespace osc\api\controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/3/19
 * Time: 14:18
 */

use think\Db;
use think\Request;

class Login extends Base
{

    private $member;

    public function __construct()
    {
        parent::__construct();
        //用户模型
        $this->member       =   new \osc\api\model\Member();

    }

  
    /**
     * 登录接口
     * @return string
     */
    public function login(){

        if($this->request->isPost()){
            $post_data = $this->request->post();

            if(!$post_data){
                return json_encode(['check'=>0, 'mage'=>'提交错误!']);
            }

            //登录验证
            $r=osc_service('common','Member')->api_login($post_data);

            if(isset($r['error'])){
                return json_encode($r);
            }elseif($r['success']){

                return json_encode(['check'=>1,'mage'=>'登录成功','data'=>$r['data']]);
            }

        }else{
            return json_encode(['check'=>-2, 'mage'=>'提交错误!']);
        }
    }


    /**
     * 注册
     * @return array|mixed
     */
    function reg(){
        if(request()->isPost()){

            $data=input('post.');

            $user = $this->member->where('username',$data['username'])->count();

            if($user > 0){
                return json_encode(['mage'=>'当前号码已注册','check'=>0]);
            }

            if($data['code'] != session('mobile_code')){
                return json_encode(['mage'=>'验证码错误','check'=>0]);
            }

            if($data['password'] != $data['pwd2']){
                return json_encode(['mage'=>'两次密码不相同','check'=>0]);
            }

            if($data['2password'] != $data['2pwd2']){
                return json_encode(['mage'=>'两次二级密码不相同','check'=>0]);
            }

            $member = [
                'username' => $data['username'],
                'password' => think_ucenter_encrypt($data['password'],config('PWD_KEY')),
                '2password' => think_ucenter_encrypt($data['2password'],config('PWD_KEY')),
                'sharemobile' => $data['sharemobile'],
                'nickname' => $data['nickname'],
                'reg_type' => 'app',
//                'groupid' => config('default_group_id'),
                'regdate' => time(),
                'lastdate' => time(),
            ];
            //是否需要审核
            if(1==config('reg_check')){//需要审核或者验证
                $member['checked']=0;
            }else{
                $member['checked']=1;
            }

            $uid = $this->member->insertGetId($member);

            if($uid){

                storage_user_action($uid,$member['username'],config('FRONTEND_USER'),'注册成为会员');
                if(1==config('reg_check')){
                    return json_encode(['success'=>'注册成功，请等待管理员审核','check'=>1]);
                }else{
                    return json_encode(['success'=>'注册成功','check'=>1]);
                }

            }else{
                return json_encode(['mage'=>'注册失败，请重新尝试','check'=>0]);
            }
        }
        return json_encode(['mage'=>'提交错误，请重新尝试','check'=>0]);
    }



}